#pragma once
#include "log.hpp"

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <cstring>

#define NUM 1024

namespace Server
{
    enum
    {
        USAGE_ERR = 1,
        SOCKET_ERR,
        BIND_ERR,
        LISTEN_ERR
    };

    static const uint16_t gport = 8080;
    static const int gbacklog = 5;

    class TcpServer
    {
    public:
        TcpServer(const uint16_t &port = gport)
            : _port(port), _listensock(-1)
        {
        }
        void initServer()
        {
            // 1.创建socket套接字
            _listensock = socket(AF_INET, SOCK_STREAM, 0);
            if (_listensock < 0)
            {
                logMessage(FATAL, "create socket error!");
                exit(SOCKET_ERR);
            }
            logMessage(NORMAL, "create socket success!");
            // 2.bind自己的网络信息
            struct sockaddr_in local;
            local.sin_family = AF_INET;
            local.sin_port = htons(_port);
            local.sin_addr.s_addr = INADDR_ANY;
            if (bind(_listensock, (struct sockaddr *)&local, sizeof(local)) < 0)
            {
                logMessage(FATAL, "bind socket error!");
                exit(BIND_ERR);
            }
            logMessage(NORMAL, "bind socket success!");
            // 3.设置socket listen监听状态
            if (listen(_listensock, gbacklog) < 0)
            {
                logMessage(FATAL, "listen socket error!");
                exit(LISTEN_ERR);
            }
            logMessage(NORMAL, "listen socket success!");
        }
        void run()
        {
            for (;;)
            {
                // 4.获取链接
                struct sockaddr_in peer;
                socklen_t len = sizeof(peer);
                int sock = accept(_listensock, (struct sockaddr *)&peer, &len);
                if (sock < 0)
                {
                    logMessage(ERROR, "accept error,next!");
                    continue;
                }
                logMessage(NORMAL, "accept a new link success!");

                std::cout << "sock: " << sock << std::endl;
                serviceIO(sock); // 面向字节流，后续都是对文件的操作
                close(sock);
            }
        }
        void serviceIO(int sock)
        {
            char buffer[NUM];
            while (true)
            {
                ssize_t s = read(sock, buffer, sizeof(buffer) - 1);
                // 目前读到的数据当成字符串
                if (s > 0)
                {
                    buffer[s] = 0;
                    std::cout << "recv message: " << buffer << std::endl;

                    // 返回数据
                    std::string outbuffer = buffer;
                    outbuffer += " server [echo]";
                    write(sock, outbuffer.c_str(), outbuffer.size());
                }
                else if (s == 0)
                {
                    // 说明客户端已经关闭
                    logMessage(NORMAL, "client quie, me too!");
                    break;
                }
            }
        }
        ~TcpServer()
        {
        }

    private:
        int _listensock;
        uint16_t _port;
    };
}

